﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace CProd
{
    public partial class frmTarefas : Form
    {
        #region Declarações

        SqlDataAdapter daTarefas = new SqlDataAdapter("Select * from Tarefas", frmEntrada.conn);
        SqlCommandBuilder Builder = new SqlCommandBuilder();
        DataTable dtTarefas = new DataTable("dtTarefas");
        DataView dvTarefas = new DataView();
        BindingSource srcTarefas = new BindingSource();
        Boolean newRecord;

        #endregion

        public frmTarefas()
        { InitializeComponent(); }

        private void BtnSairClick(object sender, EventArgs e)
        { Close(); }

        private void FrmTarefasLoad(object sender, EventArgs e)
        {
            Builder.DataAdapter = daTarefas;
            daTarefas.InsertCommand = Builder.GetInsertCommand(true);

            daTarefas.Fill(dtTarefas);

            srcTarefas.DataSource = dtTarefas;
            dvTarefas.Table = dtTarefas;

            txtCodigo.DataBindings.Add("Text", srcTarefas, "Tare_Cod");
            txtDesignacao.DataBindings.Add("Text", srcTarefas, "Tare_Des");
            txtObs.DataBindings.Add("Text", srcTarefas, "Tare_Obs");

            GridTarefas.DataSource = srcTarefas;
            GridTarefas.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;

            GridTarefas.Columns[0].HeaderText = "Código";
            GridTarefas.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            GridTarefas.Columns[1].HeaderText = "Designação";

            GridTarefas.Columns[2].HeaderText = "Observações";
            GridTarefas.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            GridTarefas.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            GridTarefas.Columns[2].Visible = false;
        }

        private void BtnFirstClick(object sender, EventArgs e)
        { srcTarefas.MoveFirst(); }

        private void BtnLastClick(object sender, EventArgs e)
        { srcTarefas.MoveLast(); }

        private void BtnPrevClick(object sender, EventArgs e)
        { srcTarefas.MovePrevious(); }

        private void btNext_Click(object sender, EventArgs e)
        { srcTarefas.MoveNext(); }

        private void btInsert_Click(object sender, EventArgs e)
        {
            txtCodigo.Enabled = true;
            txtCodigo.ReadOnly = false;
            txtCodigo.Clear();
            txtDesignacao.Clear();
            txtObs.Clear();

            txtCodigo.Focus();
            newRecord = true;
        }

        private void BtnSaveClick(object sender, EventArgs e)
        {
            if (txtCodigo.ReadOnly == false) txtCodigo.ReadOnly = true;

            try
            {
                #region novo registo

                if (newRecord)
                {
                    dvTarefas.RowFilter = string.Format("Tare_Cod Like '{0}'", txtCodigo.Text);
                    if (dvTarefas.Count > 0) { MessageBox.Show("Tarefa já existente"); newRecord = false; }
                    else
                    {
                        DataRow dRow = dtTarefas.NewRow();
                        dRow[0] = txtCodigo.Text;
                        dRow[1] = txtDesignacao.Text;
                        dRow[2] = txtObs.Text;
                        dtTarefas.Rows.Add(dRow);
                        newRecord = false;
                    }
                }
                #endregion
                else
                    BindingContext[srcTarefas].EndCurrentEdit();
                daTarefas.Update(dtTarefas);

                //Actualiza Dados Mostrados
                dtTarefas.Clear();
                daTarefas.Fill(dtTarefas);
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }

        private void BtnDeleteClick(object sender, EventArgs e)
        {
            if (MessageBox.Show("Tem a certeza?", "Apagar Registo", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                try
                {
                    srcTarefas.RemoveCurrent();
                    daTarefas.Update(dtTarefas);
                    MessageBox.Show("Registo Apagado com Sucesso");
                }
                catch (Exception ex)
                { MessageBox.Show(ex.Message); }
            }
        }

        private void BtnImprimirClick(object sender, EventArgs e)
        {
            DGVPrinter printer = new DGVPrinter
            {
                Title = "Listagem de Tarefas",
                PorportionalColumns = true,
                Footer = "Filkemp",
                FooterSpacing = 15
            };

            printer.ColumnWidths.Add(GridTarefas.Columns[1].Name, 100);
            printer.ColumnWidths.Add(GridTarefas.Columns[2].Name, 550);

            if (MessageBox.Show("Deseja ver antes?", "Pré-visualização", MessageBoxButtons.YesNo) == DialogResult.Yes)
                printer.PrintPreviewDataGridView(GridTarefas);
            else printer.PrintDataGridView(GridTarefas);
        }
    }
}
